﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; 
using WoChu.RfClient.Entity;
using WoChu.RfClient.Duty;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmPickLocationRelation : Form
    {
        public FrmPickLocationRelation()
        {
            InitializeComponent();
        }

        private static List<LocationGoodsInfo> _lstLocationGoods = null;
        public static List<LocationGoodsInfo> lstLocationGoods
        {
            get
            {
                if (_lstLocationGoods == null) _lstLocationGoods = new List<LocationGoodsInfo>();
                return _lstLocationGoods;
            }
            set { _lstLocationGoods = value; }
        }
  

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstLocationGoods != null && lstLocationGoods.Count() > 0)
                {
                    if (WoChu.CF.BaseFrame.Util.MessageBoxEx.Question("是否提交？") == DialogResult.No) return;


                    var re = Duty.PickLocationRelationUIRule.NewInstance.SetLocationGoodsPick(LoginBean.Instance.USERCODE, lstLocationGoods);

                    if (re.DATA)
                    {
                        lstLocationGoods.Clear();
                        this.bsLocationGoodsSource.DataSource = null;
                        this.dtGriLocationdGoods.DataSource = this.bsLocationGoodsSource;

                        txtGoodsCode.Text = string.Empty;
                       txtLocationCode.Text = string.Empty;
                       txtLocationCode.Focus();
                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("提交成功！");
                        lstLocationGoods = null;
                    }
                    else
                    {
                        MessageBox.Show("提交失败！" + re.MESSAGE);
                        return;
                    }
                }
            }

            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        private void txtLocationCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            Cursor.Current = Cursors.WaitCursor;
             LocationGoodsInfo _LocationGoods =null;
            try
            {
                _LocationGoods = Duty.PickLocationRelationUIRule.NewInstance.ExistLoctionGoodsByLocationCode(txtLocationCode.Text).DATA;
                 
                if (_LocationGoods == null)
                {
                    MessageBox.Show("找不到该货位信息");
                    txtLocationCode.SelectAll();
                    return;
                }
                if ( _LocationGoods.LOCATIONGOODSID > 0)
                {
                    string Message = string.Format("货位:{0}, 商品名称:{1},已存在关联 \n 是否删除该关联？", txtLocationCode.Text, _LocationGoods.GOODSNAME);
                    if (MessageBoxEx.Question(Message) == DialogResult.No)
                    {
                        txtLocationCode.SelectAll();
                        txtLocationCode.Text = string.Empty;
                        return;
                    }
                    if (!Duty.PickLocationRelationUIRule.NewInstance.DeleteLoctionGoodsRelation(_LocationGoods.LOCATIONGOODSID).DATA)
                    {
                        MessageBox.Show("删除失败，请重试！");
                        txtLocationCode.SelectAll();
                        return;
                    }
                    else
                    {
                        _LocationGoods = new LocationGoodsInfo() { LOCATIONCODE = _LocationGoods.LOCATIONCODE, LOCATIONID = _LocationGoods.LOCATIONID,GOODSNAME="",GOODSCODE="" };
                    }
                }
                 
                int curIndex = lstLocationGoods.FindIndex(x => x.LOCATIONID == _LocationGoods.LOCATIONID);
                if (curIndex >= 0)
                {
                    this.dtGriLocationdGoods.Select(curIndex);
                }
                else
                {
                    lstLocationGoods.Add(_LocationGoods);
                    this.bsLocationGoodsSource.DataSource = null;
                    this.bsLocationGoodsSource.DataSource = lstLocationGoods;
                    this.dtGriLocationdGoods.DataSource = this.bsLocationGoodsSource;
                    int  index = lstLocationGoods.FindIndex(x => x.LOCATIONID == _LocationGoods.LOCATIONID);
                    if (index >= 0)
                    {
                        this.dtGriLocationdGoods.Select(index);
                    }
                }
              
              txtGoodsCode.SelectAll();
              txtGoodsCode.Focus();
            }

            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void txtGoodsCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                string locationCode = txtLocationCode.Text;
                if (string.IsNullOrEmpty(locationCode))
                {
                    MessageBox.Show("请先扫描货位！");
                    txtLocationCode.SelectAll();
                    return;
                }
                if (!txtGoodsCode.Text.StartsWith("WOC"))
                {
                    MessageBox.Show("请扫描WOC开头的商品！");
                    txtGoodsCode.SelectAll();
                    return;
                }

                LocationGoodsInfo _LocationGoods = Duty.PickLocationRelationUIRule.NewInstance.ExistLoctionGoodsByGoodsCode(txtGoodsCode.Text).DATA;
                
                if (_LocationGoods == null)
                {
                    MessageBox.Show("找不到该商品信息");
                    txtLocationCode.SelectAll();
                    return;
                }
                if (_LocationGoods != null && _LocationGoods.LOCATIONGOODSID > 0)
                {
                    string Message = string.Format("商品:{0}, 货位:{1},已存在关联 \n 是否删除该关联？", _LocationGoods.GOODSNAME, _LocationGoods.LOCATIONCODE);
                    if (MessageBoxEx.Question(Message) == DialogResult.No)
                    {
                        txtGoodsCode.SelectAll();
                        txtGoodsCode.Text = string.Empty;
                        return;
                    }
                    if (!Duty.PickLocationRelationUIRule.NewInstance.DeleteLoctionGoodsRelation(_LocationGoods.LOCATIONGOODSID).DATA)
                    {
                        MessageBox.Show("删除失败，请重试！");
                        txtGoodsCode.SelectAll();
                        return;
                    }
                }
                var curIndex = lstLocationGoods.FindIndex(x => x.LOCATIONCODE == locationCode);
                if (curIndex < 0)
                {
                    MessageBox.Show("货位记录不存在，请先扫描货位！");
                    return;
                }
                lstLocationGoods[curIndex].GOODSNAME = _LocationGoods.GOODSNAME;
                lstLocationGoods[curIndex].GOODSID = _LocationGoods.GOODSID;
                lstLocationGoods[curIndex].GOODSCODE = _LocationGoods.GOODSCODE;
                //lstLocationGoods[curIndex].GOODSID3 = _LocationGoods.GOODSID;
                //lstLocationGoods[curIndex].GOODSCODE3 = _LocationGoods.GOODSCODE;
                this.bsLocationGoodsSource.DataSource = null;
                this.bsLocationGoodsSource.DataSource = lstLocationGoods;
                this.dtGriLocationdGoods.DataSource = this.bsLocationGoodsSource;
                this.dtGriLocationdGoods.Refresh();
                this.dtGriLocationdGoods.Select(curIndex);
                txtLocationCode.Text = string.Empty;
                txtGoodsCode.Text = string.Empty;
                txtLocationCode.Focus();
            }

            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }

        }

        private void dtGriLocationdGoods_Click(object sender, EventArgs e)
        {
            int curIndex = dtGriLocationdGoods.CurrentRowIndex;
            if (curIndex < 0) return;
            txtLocationCode.Text = dtGriLocationdGoods[curIndex, 0].ToString();
            txtGoodsCode.Text = dtGriLocationdGoods[curIndex, 2].ToString();
            this.dtGriLocationdGoods.Select(curIndex);
        }

        private void dtGriLocationdGoods_DoubleClick(object sender, EventArgs e)
        {
            int curIndex = dtGriLocationdGoods.CurrentRowIndex;
            if (curIndex < 0) return;
            try
            {
                string locationCode = dtGriLocationdGoods[curIndex, 0].ToString();
                LocationGoodsInfo curItem =lstLocationGoods.Find(x => x.LOCATIONCODE ==locationCode);

                string Message = string.Format("名称:{0}, 货位:{1} \n 是否删除该数据？", curItem.GOODSNAME, curItem.LOCATIONCODE);
                if (MessageBoxEx.Question(Message) == DialogResult.Yes)
                {
                    lstLocationGoods.Remove(curItem);
                    this.bsLocationGoodsSource.DataSource = null;
                    this.bsLocationGoodsSource.DataSource = lstLocationGoods;
                    this.dtGriLocationdGoods.DataSource = this.bsLocationGoodsSource;
                    this.dtGriLocationdGoods.Refresh();
                }
            }
            catch (Exception ex)
            {
                TraceEx.Write(ex.Message);
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            }
            finally
            {
                txtGoodsCode.Focus();
                txtGoodsCode.SelectAll();
            }
        }
    }
}